package jwdt.xadmin.rskq.service.impl;

import java.util.List;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.rskq.bean.TKqjb;
import jwdt.xadmin.rskq.service.KqjbManageService;
import jwdt.xadmin.system.bean.TUser;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;
import com.ezerg.web.Form;
import com.ezerg.web.Module;

public class KqjbManageServiceImpl implements KqjbManageService {

	private BaseDao<TKqjb> baseDao;
	
	public void setBaseDao(BaseDao<TKqjb> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil doList(PageUtil pu, TKqjb tc ,String flag) throws Exception{
		String where = "";
		String odatetime=tc.getOdatetime();
		if(!StringUtil.nullOrBlank(odatetime)) {
			where += "odatetime BETWEEN '"+odatetime+" 00:00:00' AND '"+odatetime+" 23:59:59'";
		}
		if(!StringUtil.nullOrBlank(flag)) {
			if(!StringUtil.nullOrBlank(odatetime))
			{
				where+=" and";
			}
			where += " flag='"+flag+"'";
		}
		String sql = " order by id desc";
		pu.doList(TKqjb.class,where+sql);
		//创造json数据对象
		return pu;
	}

	@Override
	public List kqjbcheck(String flag) throws Exception{
		return Jdb.get().query("SELECT * FROM (SELECT * FROM t_kq_jbsq a LEFT JOIN (SELECT fullname AS xapplicant,UserId FROM t_user ) b ON a.applicant=b.UserId) a LEFT JOIN (SELECT fullname AS xleader,UserId FROM t_user ) b ON a.leader=b.UserId where flag='"+flag+"'");
	}
	
	@Override
	public String kqjbAddact(TKqjb obj,TUser userInfo) throws Exception{
		obj.setOdatetime(DateUtil.getDateTime());
		obj.setFlag("0");
		obj.setApplicant(userInfo.getUserid());
		return baseDao.doAddObj(obj);
	}
	
	@Override
	public String kqjbEditact(TKqjb obj,int id) throws Exception{
		return baseDao.doUpdateObj(TKqjb.class,id,obj);
	}
	
	@Override
	public String kqjbDel(int id) throws Exception{
		return baseDao.doDelObj(TKqjb.class,id);
	}
	
	@Override
	public String kqjbSpb(Form f, Module m,int id) throws Exception{
		TKqjb obj = (TKqjb) Jdb.get().get(TKqjb.class, id);
		obj.setFlag("2");
		obj = (TKqjb) f.copy2Obj(obj);
		return baseDao.doUpdateObj(TKqjb.class,id,obj);
	}
	
	@Override
	public String kqjbSpt(Form f, Module m,int id) throws Exception{
		
		TKqjb obj = (TKqjb) Jdb.get().get(TKqjb.class, id);
		obj.setFlag("1");
		obj = (TKqjb) f.copy2Obj(obj);
		return baseDao.doUpdateObj(TKqjb.class,id,obj);
	}
	
}
